package com.moore.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.moore.model.Goods;
import com.moore.model.SecKill;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class GoodsUtils {
    public static String getGoods(String state,String name,String classify, String... ids){
        String sql = "select * from goods where 1=1 ";
        if (state != null) {
            sql += " and state = " + state;
        }
        if (name != null && !name.equals("0")) {
            sql += " and name like '%" + name + "%'";
        }
        if (classify != null && !classify.equals("0")){
            sql += " and classify = "+ classify;
        }
        if (ids != null && !Objects.equals(ids[0], "0")) {
            StringBuilder sqlBuilder = new StringBuilder(sql);
            if (ids.length > 1){
                sqlBuilder.append("and id = ").append(ids[0]);
                for (int i = 1; i < ids.length; i++){
                    sqlBuilder.append(" or id = ").append(ids[i]);
                }
            } else {
                sqlBuilder.append(" and id = ").append(ids[0]);
            }
            sql = sqlBuilder.toString();
        }
        List<String> list;
        List<String> jsonList = new ArrayList<>();
        try {
            list = JDBCUtils.executeSelect(sql,"id","name","image","price","inventory","classify","state");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        int i = 0;
        while(true) {
            // 检查是否超出范围
            if (7 * i + 6 >= list.size()) {
                break; // 如果超出范围，跳出循环
            }
            Goods goods = new Goods(
                    Integer.parseInt(list.get(7 * i)),
                    list.get(7 * i + 1),
                    list.get(7 * i + 2),
                    Double.parseDouble(list.get(7 * i + 3)),
                    Integer.parseInt(list.get(7 * i + 4)),
                    Integer.parseInt(list.get(7 * i + 5)),
                    Integer.parseInt(list.get(7 * i + 6))
            );
            i++;
            jsonList.add(JSON.toJSONString(goods));
        }
        return JSON.toJSONString(jsonList);
    }

    public static String getGoodsImage(String id){
        String sql = "select image from images where goods_id = " + id;
        List<String> list;
        try {
             list = JDBCUtils.executeSelect(sql,"image");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return JSON.toJSONString(list);
    }

    public static List<String> getSecKillGoods(String value,String value1){
        String sql = "select "+ value +" from activitys where start_time = '" + value1 +"'";
        List<String> list;
        try {
            list = JDBCUtils.executeSelect(sql,value);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    public static String getSecKillGoods(String value) {
        String sql = "select * from activitys ";
        if (value != null && !value.isEmpty()) {
            sql += "where goods_id = " + value;
        } else {
            sql += "where 1 = 1";
        }
        List<String> list;
        try {
            list = JDBCUtils.executeSelect(sql,"id","goods_id","start_time","end_time","price","limit","number");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        List<String> jsonList = new ArrayList<>();
        int i = 0;
        while(true) {
            // 检查是否超出范围
            if (7 * i + 6 >= list.size()) {
                break; // 如果超出范围，跳出循环
            }
            SecKill secKill = new SecKill(
                    list.get(7 * i),
                    list.get(7 * i + 1),
                    list.get(7 * i + 2),
                    list.get(7 * i + 3),
                    list.get(7 * i + 4),
                    list.get(7 * i + 5),
                    list.get(7 * i + 6)
            );
            i++;
            jsonList.add(JSON.toJSONString(secKill));
        }
        return JSON.toJSONString(jsonList);
    }

    public static String getNames(String id) {
        String sql = "select id,name from goods where state = 0";
        if (id == null || id.isEmpty()) {
            sql += " and 1 = 1";
        }  else {
            sql += " and id = " + id;
        }
        List<String> list;
        try {
            list = JDBCUtils.executeSelect(sql,"id","name");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        JSONObject jsonObject = new JSONObject();
        int i = 0;
        while(true) {
            // 检查是否超出范围
            if (2 * i + 1 >= list.size()) {
                break; // 如果超出范围，跳出循环
            }
            jsonObject.put(list.get(2 * i),list.get(2 * i + 1));
            i++;
        }
        return JSON.toJSONString(jsonObject);
    }
}
